﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KD.Sms.UI_Manager
{
    public partial class frmSysParms : Form
    {
        public frmSysParms()
        {
            InitializeComponent();
        }

        private void frmSysParms_Load(object sender, EventArgs e)
        {
            cbButtomUp.Checked = CacheSystemControl.E_ButtomUp;

            this.Enabled = CacheUser.E_IsAdmin;

            DataTable _tabAdminState = DAL.SqlLite.Main.Instance.DoQueryAdminState();
            cbLockAdmin.Checked = _tabAdminState != null && _tabAdminState.Rows.Count == 1 && _tabAdminState.Rows[0]["User_State"].ToString() == "1";

            cbUnmanned.Checked = ConfigurationManager.AppSettings["Unmanned"].ToIntEx() == 1;
            cbUnmanned.CheckedChanged += cbUnmanned_CheckedChanged_;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbButtomUp_CheckedChanged(object sender, EventArgs e)
        {
            var _Msg = string.Format("是否确定{0}触底反弹功能", cbButtomUp.Checked ? "启用" : "停用");
            if (MessageBox.Show(_Msg, "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (Task.Cyj.Instance.DoWrite(410, cbButtomUp.Checked ? (short)110 : (short)0))
                {
                    MessageBox.Show("操作成功!");
                    DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Operate, string.Format("设定{0}触底反弹功能", cbButtomUp.Checked ? "启用" : "停用"));
                }
                else
                    MessageBox.Show("操作失败!");
            }
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbLockAdmin_CheckedChanged(object sender, EventArgs e)
        {
            var _Msg = string.Format("是否{0}管理员禁止采样", cbLockAdmin.Checked ? "锁定" : "释放");
            if (DAL.SqlLite.Main.Instance.DoUpdateAdminState(cbLockAdmin.Checked))
            {
                DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Operate, string.Format("{0}管理员禁止采样", cbLockAdmin.Checked ? "锁定" : "释放"));
            }
        }

        /// <summary>
        /// 修改配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static bool AddOrUpdateAppSettings(string key, string value)
        {
            try
            {
                var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var settings = configFile.AppSettings.Settings;
                if (settings[key] == null)
                {
                    settings.Add(key, value);
                }
                else
                {
                    settings[key].Value = value;
                }
                configFile.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
                return true;
            }
            catch (ConfigurationErrorsException)
            {
                return false;
            }
        }

        private void cbUnmanned_CheckedChanged_(object sender, EventArgs e)
        {

            var _Msg = string.Format("是否{0}无人值守车型数据模拟", cbUnmanned.Checked ? "开启" : "关闭");
            if (MessageBox.Show(_Msg, "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (AddOrUpdateAppSettings("Unmanned", cbUnmanned.Checked ? "1" : "0"))
                {
                    MessageBox.Show("操作成功，必须重启软件才能生效!");
                    DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Operate, string.Format("{0}无人值守车型数据模拟", cbLockAdmin.Checked ? "开启" : "关闭"));
                }
                else
                {
                    MessageBox.Show("操作失败!");
                }
            }
            
        }
    }
}
